package com.txunda.zhuohired.aty.user.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshBase;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshListView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.my.PriceDetailAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.my.PriceDetailBean;
import com.txunda.zhuohired.http.pst.InfoPst;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/8 14:01
 * 功能描述：账户明细
 * 联系方式：jiahuanchen@126.com
 */

public class PriceDetailAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * listView
     */
    @ViewInject(R.id.lv_price_detail)
    private PullToRefreshListView lv_price_detail;
    /**
     * 空数据
     */
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private PriceDetailAdapter priceDetailAdapter;
    private InfoPst infoPst;
    private String token = "";
    private int page = 1;
    private List<PriceDetailBean.DataBean> priceDetailList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("账户明细");
        lv_price_detail.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            /**
             * 下拉刷新
             * @param refreshView
             */
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initHttp();//请求接口
            }

            /**
             * 上拉加载更多
             * @param refreshView
             */
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = page + 1;
                initHttp();//请求接口
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_price_detail;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        infoPst = new InfoPst(this);
        priceDetailList = new ArrayList<>();
    }

    private void initHttp() {
        infoPst.userMoney(token, String.valueOf(page));
    }

    @Override
    protected void requestData() {
        initHttp();
    }

    private void initAdapter() {
        priceDetailAdapter = new PriceDetailAdapter(this, priceDetailList);
        lv_price_detail.setAdapter(priceDetailAdapter);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("userMoney")) {
            L.e("==========userMoney==========", jsonStr);
            PriceDetailBean priceDetailBean = GsonUtil.GsonToBean(jsonStr, PriceDetailBean.class);

            if (page == 1) {
                priceDetailList = priceDetailBean.getData();
                if (priceDetailList.size() == 0) {
                    noData(2);
                } else {
                    noData(1);
                    initAdapter();
                }
            } else {
                if (priceDetailBean.getData().size() == 0) {
                    showToast("已经是最后一页，没有数据了");
                } else {
                    List myPriceList = priceDetailBean.getData();
                    priceDetailList.addAll(myPriceList);
                    priceDetailAdapter.notifyDataSetChanged();
                }
            }
            return;
        }
    }

    private void noData(int type) {
        if (type == 1) {
            lv_price_detail.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        } else {
            lv_price_detail.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        }
    }
}
